package com.yazi.mxz.person.controller.shop;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;




import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.myframework.smhj.common.exception.MyException;
import com.myframework.smhj.hibernate.Page;
import com.myframework.smhj.hibernate.Searching;
import com.myframework.smhj.hibernate.Sort;
import com.myframework.smhj.jackson.result.ObjectResult;
import com.myframework.smhj.jackson.result.StatusResult;
import com.yazi.mxz.common.entity.beans.Store;
import com.yazi.mxz.common.entity.beans.StoreGift;
import com.yazi.mxz.common.entity.beans.StorePromote;
import com.yazi.mxz.common.entity.enums.StorePromoteStatus;
import com.yazi.mxz.common.service.StorePromoteService;

@Controller("shop-storePromoteController")
@RequestMapping("/shop/storePromote")
public class StorePromoteController extends BaseController {
	private static Logger log = LoggerFactory.getLogger(StorePromoteController.class);
	
	@Resource
	StorePromoteService storePromoteService;
	
	@RequestMapping("/list")
	public String list(StorePromote storePromote,Searching si, Model model) {
		debug(log, "进入满额促销列表页面");
		
		// 默认按促销开始时间顺序
		if (null == si.getSorts()) {
			List<Sort> sorts = new ArrayList<Sort>();
			sorts.add(new Sort("startTime"));
			si.setSorts(sorts);
		}
		
		// 只查询当前店铺
		Store store = new Store();
		store.setStoreId(getStoreId());
		storePromote.setStore(store);
		
		Page<StorePromote> page = storePromoteService.list(storePromote, si);
		// 促销状态
		StorePromoteStatus[] spStatuses = StorePromoteStatus.values();
		
		model.addAttribute("list", page.getList());
		model.addAttribute("spStatuses", spStatuses);
		model.addAttribute("storePromote", storePromote);
		model.addAttribute("remain", getStore(true).getPromRemain());
		
		return "shop/storePromote_list";
	}
	
	/**
	 * 
	 * @param promoteId
	 * @param saleType 0(Online)线上,1(Local)线下
	 * @param model
	 * @return
	 */
	@RequestMapping("/addOrEdit")
	public String addOrEdit(Long promoteId, Integer saleType, HttpServletRequest request, Model model) {
		debug(log, "进入满额促销添加/编辑页面");
		StorePromote sp = null;
		if (null != promoteId) {
			sp = storePromoteService.one(promoteId);
			if (null == sp || !sp.getStore().equals(getStore(true))) {
				return REDIRECT_INDEX;
			}
		}
		model.addAttribute("sp", sp);
		model.addAttribute("saleType", saleType);
		
		return "shop/storePromote_addOrEdit";
	}
	
	@RequestMapping("/addOrEditDo/json")
	@ResponseBody
	public ObjectResult<?> addOrEditDoJson(StorePromote storePromote, Gifts gifts) {
		ObjectResult<Long> jsonResult = new ObjectResult<Long>();
		try {
			if (null != storePromote.getPromoteId()) {
				debug(log, "保存编辑过的满额促销");
				// 防止篡改属性
				storePromote.setType(null);
				storePromoteService.editByStore(getStore(true), storePromote, gifts.getGifts());
			} else {
				debug(log, "保存新添加的满额促销");
				// 强制与店铺绑定
				Store store = new Store();
				store.setStoreId(getStoreId());
				storePromote.setStore(store);
				
				storePromoteService.addByStore(getStore(true), storePromote, gifts.getGifts());
			}
			jsonResult.setResult(storePromote.getPromoteId());
			jsonResult.setOk();
		} catch (MyException e) {
			debug(log, "保存满额促销失败：" + e.getMessage());
			jsonResult.setFailMessage(e.getMessage());
		} catch (Exception e) {
			error(log, e, "保存满额促销异常");
			jsonResult.setErrorMessage("保存失败，请联系管理员");
		}
		
		return jsonResult;
	}
	
	@RequestMapping("/delDo/json")
	@ResponseBody
	public StatusResult delDoJson(long promoteId) {
		debug(log, "删除满额促销信息");
		StatusResult jsonResult = new StatusResult();
		try {
			storePromoteService.removeByStore(getStore(true), promoteId);
			
			jsonResult.setOkMessage("删除成功");
		} catch (Exception e) {
			error(log, e, "删除满额促销信息异常");
			jsonResult.setErrorMessage("删除失败，请联系管理员");
		}
		return jsonResult;
	}
	
	@RequestMapping("/statusDo/json")
	@ResponseBody
	public ObjectResult<?> statusDoJson(long promoteId, StorePromoteStatus status) {
		debug(log, "更新促销活动状态");
		ObjectResult<Integer> jsonResult = new ObjectResult<Integer>();
		try {
			storePromoteService.editStatusByStore(getStore(true), promoteId, status);
			jsonResult.setResult(status.ordinal());
			jsonResult.setOk();
		} catch (MyException e) {
			jsonResult.setFailMessage(e.getMessage());
		} catch (Exception e) {
			error(log, e, "更新促销活动状态异常");
			jsonResult.setErrorMessage("操作失败，请联系管理员");
		}
		return jsonResult;
	}
	
	public static class Gifts {
		private List<StoreGift> gifts;

		public List<StoreGift> getGifts() {
			return gifts;
		}

		public void setGifts(List<StoreGift> gifts) {
			this.gifts = gifts;
		}

	}
	
}
